package cn.kgc.springboot.vue.controller;

import cn.kgc.springboot.vue.commons.QueryCondition;
import cn.kgc.springboot.vue.entity.Course;
import cn.kgc.springboot.vue.exception.BusinessException;
import cn.kgc.springboot.vue.response.ResponseResult;
import cn.kgc.springboot.vue.service.CourseService;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author 课工场
 * @date 2024/12/27
 * @description
 */
@RestController
@RequestMapping("course")
public class CourseController {

    @Resource
    private CourseService courseService;

    @GetMapping("list")
    public ResponseResult courseList(){
        return  ResponseResult.success().data("courseList",courseService.list());
    }

    @PostMapping("page")
    public ResponseResult  coursePage(@RequestBody QueryCondition<Course> queryCondition){

       return courseService.coursePage(queryCondition);
    }


    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateCourse(@RequestBody Course course){

        try {
            courseService.saveOrUpdate(course);
            return  ResponseResult.success();
        }catch (DuplicateKeyException e){
            throw  new BusinessException("课程名或编号重复");
        }
    }

    @DeleteMapping("delete/{id}")
    public ResponseResult deleteCourse(@PathVariable("id") Integer id){
       return courseService.deleteCoure(id);
    }


    @PutMapping("changeState")
    public ResponseResult  changeState(@RequestBody HashMap<String,Integer> params){

        return  courseService.changeState(params);
    }
}
